HeroData = {}
HeroDataFunc = {}
HeroDataFunc.__index = HeroDataFunc
setmetatable(HeroData, HeroDataFunc)

HeroDataFunc.create = function(self, heroIndex)
  print("HeroDataFunc.create", heroIndex)
  local hd = {}
  setmetatable(hd, HeroDataFunc)
  hd.heroIndex = heroIndex
  hd.heroInfo = UserData.userinfo.host.armyGroups[tostring(heroIndex)]
  hd.heroConfig = UserData.config.models.General[heroIndex]
  hd.equips = {}
  if hd.heroInfo~=nil then
  for k,v in pairs(hd.heroInfo.e) do
    if v~=0 then
      local eq = Equip:create(v)
      hd.equips[#hd.equips+1] = eq
    end
  end
  end

  hd.equipPros = {normalAttack=0, tacticsAttack=0, blastAttack=0, normalDefense=0,
                  tacticsDefense=0, blastDefense=0, forces=0}
  for k,v in pairs(hd.equips) do
    local key, desc, attr = v:getAttr1()
    if hd.equipPros[key]==nil then
      hd.equipPros[key] = attr
    else
      hd.equipPros[key] = hd.equipPros[key]+attr
    end

    key, desc, attr = v:getAttr2()
    if desc~=nil then
      if hd.equipPros[key]==nil then
        hd.equipPros[key] = attr
      else
        hd.equipPros[key] = hd.equipPros[key]+attr
      end
    end 
  end
  for k,v in pairs(hd.equipPros) do 
    print(k, v)
  end
  return hd
end

HeroDataFunc.createTip = function(self)
  print("HeroDataFunc.createTip")
  local tipLayer = CCBReader:load("HeroProperty.ccbi")
  
  setObjectExtTable(tipLayer:getChildByTag(1111), self)
  setObjectExtTable(tipLayer:getChildByTag(1020), self)
  setObjectExtTable(tipLayer:getChildByTag(1021), self)

  local lv = 0
  if self.heroInfo~=nil then
    lv = self.heroInfo.l
  end
  TypeCast(tipLayer:getChildByTag(2000), "CCLabelTTF"):setString(string.format("%s Lv%d", self.heroConfig.name, lv))  
  TypeCast(tipLayer:getChildByTag(2000), "CCLabelTTF"):setColor(Equip.EquipColor[self.heroConfig.quality])

  TypeCast(tipLayer:getChildByTag(2002), "CCLabelTTF"):setString(string.format("%d", self.heroConfig.initCommander))
  TypeCast(tipLayer:getChildByTag(2003), "CCLabelTTF"):setString(string.format("%d", self.heroConfig.initTactics))  
  TypeCast(tipLayer:getChildByTag(2004), "CCLabelTTF"):setString(string.format("%d", self.heroConfig.initBlast))  
  
  local skills = UserData.config.models.NewSkill
  TypeCast(tipLayer:getChildByTag(2005), "CCLabelTTF"):setString(self.heroConfig.desc)  
  TypeCast(tipLayer:getChildByTag(2006), "CCLabelTTF"):setString(skills[self.heroConfig.skill].name)  
  TypeCast(tipLayer:getChildByTag(2007), "CCLabelTTF"):setString(autoEnter(skills[self.heroConfig.skill].desc, 39))  
 
  TypeCast(tipLayer:getChildByTag(2011), "CCLabelTTF"):setString(string.format("+%d", self.equipPros.normalAttack)) 
  TypeCast(tipLayer:getChildByTag(2012), "CCLabelTTF"):setString(string.format("+%d", self.equipPros.normalDefense)) 
  TypeCast(tipLayer:getChildByTag(2013), "CCLabelTTF"):setString(string.format("+%d", self.equipPros.tacticsAttack)) 
  TypeCast(tipLayer:getChildByTag(2014), "CCLabelTTF"):setString(string.format("+%d", self.equipPros.tacticsDefense)) 
  TypeCast(tipLayer:getChildByTag(2015), "CCLabelTTF"):setString(string.format("+%d", self.equipPros.blastAttack)) 
  TypeCast(tipLayer:getChildByTag(2016), "CCLabelTTF"):setString(string.format("+%d", self.equipPros.blastDefense)) 
  TypeCast(tipLayer:getChildByTag(2017), "CCLabelTTF"):setString(string.format("+%d", self.equipPros.forces)) 
  return tipLayer
end

HeroDataFunc.onCloseButton = function(self, node, event)
  setTouchEnabledAll(node:getParent():getParent(), true)
  node:getParent():getParent():removeChild(node:getParent(), true)
end
HeroDataFunc.onTrainButton = function(self, node, event)
  local layer = node:getParent():getParent()
  layer:removeChild(node:getParent(), true)
  local tl = JLPYLayer.getNode(self.heroIndex) 
  setTouchEnabledAll(layer, false)
  layer:addChild(tl.node)
end
